package com.wcgksoft.mobileservice.Account.plant;


import com.wcgksoft.common.util.MsgInfo;
import com.wcgksoft.entity.Plant.Plant;
import com.wcgksoft.entity.Plant.PlantEncyclopedias;
import com.wcgksoft.entity.Plant.PlantType;
import com.wcgksoft.service.GreedLand.GreedlandService;
import com.wcgksoft.service.plant.PlantEncyclopediasService;
import com.wcgksoft.service.plant.PlantService;
import com.wcgksoft.service.plant.PlantTypeService;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * 植物管理uiu
 *
 * @author Administrator
 */
@Controller
@RequestMapping("mobile/plant")
public class AppPlantController {

    @Autowired
    private PlantService plantService;
    @Autowired
    private PlantTypeService plantTypeService;
    @Autowired
    private PlantEncyclopediasService plantEncyclopediasService;

    @Autowired
    private GreedlandService greedlandService;
    /**
     * 列表单个绿地
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "list")
    public MsgInfo list(
            @RequestParam(value = "curPage", defaultValue = "1", required = false) Integer curPage,
            @RequestParam(value = "pageSize",defaultValue = "8", required = false) Integer pageSize,
            @RequestParam(value = "proid", required = false) String proid,
            @RequestParam(value = "areaid", required = false) String areaid,
            @RequestParam(value = "greedLandid", required = false) Integer greedLandid
            ) {
        MsgInfo msgInfo = new MsgInfo();
        DetachedCriteria criteria = DetachedCriteria.forClass(Plant.class);
        criteria.createAlias("greedLand", "greedLand");
         criteria.add(Restrictions.eq("greedLand.id",greedLandid));

        criteria.add(Restrictions.eq("departid", proid));
        criteria.add(Restrictions.eq("areaid", areaid));
        List<Plant> list = plantService.findByConditions(criteria,curPage-1,pageSize);

        if(list.size()>0){
            msgInfo.setCode((int) Math.ceil(list.size()/8)+1);
            msgInfo.setMsg("查询成功");
            msgInfo.setData(list);
        }else{
            msgInfo.setCode(1);
            msgInfo.setMsg("无数据");
            msgInfo.setData(list);
        }
        return msgInfo;
    }

    /**
     * 绿地植物查看
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value="view")
    public MsgInfo view(@RequestParam(value = "id") Integer id){
        MsgInfo msgInfo = new MsgInfo();
        Plant byId = plantService.findById(id);
        msgInfo.setData(byId);
        return msgInfo;
    }
    /*
     * 查看植物百科
     *
     * */
    @ResponseBody
    @RequestMapping(value = "details")
    public MsgInfo details(@RequestParam(value = "id") Integer id) {
        MsgInfo msgInfo = new MsgInfo();
        PlantEncyclopedias byId = plantEncyclopediasService.findById(id);
        msgInfo.setData(byId);
        return msgInfo;
    }


    /*
     * 单个绿地根据植物类型统计树木数量面积
     *
     * */
    @ResponseBody
    @RequestMapping(value = "typetrees")
    public MsgInfo typetrees (    @RequestParam(value = "proid", required = false) String proid,
                                  @RequestParam(value = "areaid", required = false) String areaid,
                                  @RequestParam(value = "enterpriseid", required = false) Integer enterpriseid,
            @RequestParam(value = "greedLandid", required = false) Integer greedLandid) {
        MsgInfo msgInfo = new MsgInfo();
        DetachedCriteria criteria = DetachedCriteria.forClass(Plant.class);

        criteria.add(Restrictions.eq("departid", proid));
        criteria.add(Restrictions.eq("areaid", areaid));
        criteria.createAlias("greedLand", "greedLand");
        criteria.add(Restrictions.eq("greedLand.id",greedLandid));
        List<Plant> list = plantService.findByConditions(criteria);

        DetachedCriteria criteriat = DetachedCriteria.forClass(PlantType.class);
        criteriat.add(Restrictions.eq("enterpriseid", enterpriseid));
        criteriat.add(Restrictions.eq("state",0));
        List<PlantType> listt = plantTypeService.findByConditions(criteriat);
        if(list.size()>0){
            int tnum;
            Double mian;
           for(int i=0;i<listt.size();i++){
               tnum=0;mian=0.0;
               for(int j=0;j<list.size();j++){
                    if(list.get(j).getPlantType().getId().equals(listt.get(i).getId())){
                        tnum+=list.get(j).getQuantity();
                        listt.get(i).setState(tnum);
                        mian+=list.get(j).getArea();
                        listt.get(i).setArea(mian);
                    }
               }
           }
        }
        msgInfo.setData(listt);
        return msgInfo;
    }


    /**
     * 植物管理列表（查询百科表）
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "lists")
    public MsgInfo lists(
            @RequestParam(value = "proid", required = false) String proid,
            @RequestParam(value = "areaid", required = false) String areaid,
            @RequestParam(value = "enterpriseid", required = false) Integer enterpriseid
    ) {
        MsgInfo msgInfo = new MsgInfo();
        DetachedCriteria criteria = DetachedCriteria.forClass(Plant.class);
        criteria.add(Restrictions.eq("departid", proid));
        criteria.add(Restrictions.eq("areaid", areaid));
        List<Plant> list = plantService.findByConditions(criteria);
        DetachedCriteria criteriat = DetachedCriteria.forClass(PlantEncyclopedias.class);
        criteriat.add(Restrictions.eq("enterpriseid", enterpriseid));
        List<PlantEncyclopedias> listt = plantEncyclopediasService.findByConditions(criteriat);
        List<PlantEncyclopedias> listc = new ArrayList<PlantEncyclopedias>();
        //累加
        if(list.size()>0){
            int tnum;
            for(int i=0;i<listt.size();i++){
                if(listt.size()>0){
                    tnum=0;
                    for(int j=0;j<list.size();j++){
                        if(list.get(j).getPlantEncyclopedias().getId().equals(listt.get(i).getId())){
                            tnum+=list.get(j).getQuantity();
                            listt.get(i).setNum(tnum);
                        }
                    }
                }
            }
            //去0
            if(listt.size()>0) {
                for (int i = 0; i < listt.size(); i++) {
                    if (listt.get(i).getNum()!=null) {
                        listc.add(listt.get(i));
                    }
                }
            }
        }

        msgInfo.setData(listc);
        return msgInfo;
    }


    /*
     * 植物管理 植物统计树木数量面积
     *
     * */
    @ResponseBody
    @RequestMapping(value = "typetree")
    public MsgInfo typetree ( @RequestParam(value = "proid", required = false) String proid,
                              @RequestParam(value = "areaid", required = false) String areaid,
                              @RequestParam(value = "enterpriseid", required = false) Integer enterpriseid) {
        MsgInfo msgInfo = new MsgInfo();
        DetachedCriteria criteria = DetachedCriteria.forClass(Plant.class);

        criteria.add(Restrictions.eq("departid", proid));
        criteria.add(Restrictions.eq("areaid", areaid));
        List<Plant> list = plantService.findByConditions(criteria);

        DetachedCriteria criteriat = DetachedCriteria.forClass(PlantType.class);
        criteriat.add(Restrictions.eq("enterpriseid", enterpriseid));
        criteriat.add(Restrictions.eq("state",0));
        List<PlantType> listt = plantTypeService.findByConditions(criteriat);
        if(list.size()>0){
            int tnum;
            Double mian;
            for(int i=0;i<listt.size();i++){
                tnum=0;mian=0.0;
                for(int j=0;j<list.size();j++){
                    if(list.get(j).getPlantType().getId().equals(listt.get(i).getId())){
                        tnum+=list.get(j).getQuantity();
                        listt.get(i).setState(tnum);
                        mian+=list.get(j).getArea();
                        listt.get(i).setArea(mian);
                    }
                }
            }
        }
        msgInfo.setData(listt);
        return msgInfo;
    }



}
